// pages/user/advisory/advisory.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    isIphoneX:false,
    iphoneXBottom:0,
    scrollTop:0,
    inpVal:'',    //输入框内容
    userChat:[],
    inpFocus:false,   //底部输入框是否聚焦
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let isIphoneX = app.globalData.isIphoneX;
    let userInfo = app.globalData.userInfo;
    this.setData({
      isIphoneX: isIphoneX,
      iphoneXBottom:68,
      userInfo: userInfo,
    });
    
    // 聊天内容滚动到最底部
    this.scrollToBottom();
  },
  // 用户输入的时候,获取输入的内容
  bindinput: function (e) {
    var that = this;
    let val = e.detail.value;
    that.setData({
      inpVal: val,
    })
  },
  // 失去焦点
  inputBlur: function (e) {
    var that = this;
    let val = e.detail.value;
    that.setData({
      inpFocus: false,
    })
  },
  // 发送
  sendChat:function(){
    var that = this;
    // 聊天内容滚动到最底部
    that.scrollToBottom();
    let userChat = that.data.userChat;
    let cont = that.data.inpVal;
    // 获取当前时间
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth()+1;//得到月份
    var date = now.getDate();//得到日期
    var hour = now.getHours();//得到小时
    var minu = now.getMinutes();//得到分钟
    let time = year + '-' + month + '-' + date + ' ' + hour + ':' + minu;
    const data = {
      content: cont,
      time:time,
    };
    userChat.push(data);
    console.log(time, userChat)
    this.setData({ inpVal: '', userChat: userChat });
  
  },
  // 聊天内容滚动到最底部
  scrollToBottom:function(){
    var that = this;
    const query = wx.createSelectorQuery();
    query.select('#chatCont').boundingClientRect();
    query.selectViewport().scrollOffset();
    query.exec(function (res) {
      let height = res[0].height;  //获取内容区域的高度
      that.setData({
        scrollTop: height,
      })
    })
  },
  // 发送图片
  sendPhotos:function(){
    var that = this;
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths[0]);
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})